package com.dianrui.fmi.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.dianrui.fmi.R;
import com.dianrui.fmi.bean.StockCurrent;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CurrentPostionAdapter extends BaseAdapter {
    private List<StockCurrent> list;
    private Context context;
    private CurrentPostionClickListener currentPostionClickListener;

    public CurrentPostionAdapter(Context context, List<StockCurrent> list) {
        this.list = list;
        this.context = context;
    }

    public void refresh(List<StockCurrent> list) {
        this.list = list;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    public interface CurrentPostionClickListener {
        void onBuyClick(String symbolsId);
        void onSellClick(String symbolsId);
    }

    public void setOnCurrentPostionClickListener(CurrentPostionClickListener currentPostionClickListener) {
        this.currentPostionClickListener = currentPostionClickListener;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_bond_adapter, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        if (null != list && list.size() > 0) {
            final StockCurrent stockDeal = list.get(position);
            holder.name.setText(stockDeal.name);
            holder.startPrice.setText(stockDeal.start_price);
            holder.price.setText(stockDeal.price);
            holder.yield.setText(stockDeal.yield);
            if (stockDeal.earnings.contains("-")) {
                holder.status.setText(stockDeal.earnings);
                holder.status.setTextColor(Color.GREEN);
            } else {
                holder.status.setText(stockDeal.earnings);
                holder.status.setTextColor(Color.RED);
            }
            holder.buyBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (currentPostionClickListener != null) {
                        currentPostionClickListener.onBuyClick(stockDeal.symbols_id);
                    }
                }
            });
            holder.sellBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (currentPostionClickListener != null) {
                        currentPostionClickListener.onSellClick(stockDeal.symbols_id);
                    }
                }
            });


        }
        return convertView;
    }

     class ViewHolder {
        @BindView(R.id.name)
        TextView name;
        @BindView(R.id.start_price)
        TextView startPrice;
        @BindView(R.id.price)
        TextView price;
        @BindView(R.id.status)
        TextView status;
         @BindView(R.id.yield)
         TextView yield;
        @BindView(R.id.buy_btn)
        Button buyBtn;
        @BindView(R.id.sell_btn)
        Button sellBtn;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }


}
